package com.wst.system.config.freemarker;

import freemarker.template.TemplateModelException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @Classname FreeMarkerConfig
 * @Description TODO
 * @Date 2022/3/13 16:14
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Configuration
public class FreeMarkerConfig implements ApplicationContextAware {

    @Autowired
    private freemarker.template.Configuration configuration;


    ApplicationContext applicationContext ;

    @PostConstruct
    public void setUp() throws TemplateModelException {
        // 根据注解获取bean ,key is bean name ,value is bean object
        Map<String, Object> map = this.applicationContext.getBeansWithAnnotation(FreemarkerScan.class);
        for (String key : map.keySet()) {
            configuration.setSharedVariable(key, map.get(key));
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
